if g:has_terminal == 0
    finish
endif
" --------------------------
" set  ctrl-r
" --------------------------
tmap <expr><C-r> '<C-\><C-n>"'.nr2char(getchar()).'pi'
" tab control
tnoremap <silent><M-1> <C-\><C-n>:tabn1<Cr>
tnoremap <silent><M-2> <C-\><C-n>:tabn2<Cr>
tnoremap <silent><M-3> <C-\><C-n>:tabn3<Cr>
tnoremap <silent><M-4> <C-\><C-n>:tabn4<Cr>
tnoremap <silent><M-5> <C-\><C-n>:tabn5<Cr>
tnoremap <silent><M-6> <C-\><C-n>:tabn6<Cr>
tnoremap <silent><M-7> <C-\><C-n>:tabn7<Cr>
tnoremap <silent><M-8> <C-\><C-n>:tabn8<Cr>
tnoremap <silent><M-9> <C-\><C-n>:tabn9<Cr>
tnoremap <silent><M-0> <C-\><C-n>:tablast<Cr>
" --------------------------
" open terminal
" --------------------------
if has('nvim')
    nnoremap <silent><M-t> :tabnew<Cr>:terminal<Cr>i
    tnoremap <silent><M-t> <C-\><C-n>:tabnew<Cr>:terminal<Cr>i
    command! TermPackD tabe | call termopen([&shell], {'cwd': expand('~/.leovim.d')})
    nnoremap <silent><M-h>d :TermPackD<Cr>i
else
    nnoremap <silent><M-t> :tab terminal<Cr>
    tnoremap <silent><M-t> <C-\><C-n>:tab terminal<Cr>
    nnoremap <silent><M-h>d :tab terminal<CR>cd ~/.leovim.d<tab><CR>
endif
tnoremap <silent><C-v> <C-\><C-n>
tnoremap <silent><M-q> <C-\><C-n>:ConfirmQuit<Cr>
tnoremap <silent><M-w> <C-\><C-n>:tabclose<Cr>
tnoremap <silent><M-W> <C-\><C-n>:tabonly<Cr>i
" ---------------------------------------------------------
" floaterm
" ---------------------------------------------------------
let g:floaterm_open_command = 'drop'
let g:floaterm_wintype  = 'split'
let g:floaterm_position = 'belowright'
let g:floaterm_height   = get(g:, 'asyncrun_open', 10)
let g:floaterm_keymap_prev   = '<M-{>'
let g:floaterm_keymap_next   = '<M-}>'
let g:floaterm_keymap_kill   = '<M-_>'
let g:floaterm_keymap_toggle = '<M-->'
function! s:bind_keymap(mapvar, command) abort
    if !empty(a:mapvar)
        execute printf('nnoremap <silent> %s :%s<CR>', a:mapvar, a:command)
        execute printf('tnoremap <silent> %s <C-\><C-n>:%s<CR>', a:mapvar, a:command)
    endif
endfunction
call s:bind_keymap(g:floaterm_keymap_prev,   'FloatermPrev')
call s:bind_keymap(g:floaterm_keymap_next,   'FloatermNext')
call s:bind_keymap(g:floaterm_keymap_kill,   'FloatermKill')
call s:bind_keymap(g:floaterm_keymap_toggle, 'FloatermToggle')
" key map
imap <M-{> <C-o><M-{>
imap <M-}> <C-o><M-}>
nnoremap <Tab>f :Floaterm
nnoremap <Tab>= :FloatermNew<Space>
nnoremap <Tab>- :FloatermKill<Space>
PlugAddOpt 'vim-floaterm'
" --------------------------
" new floaterm
" --------------------------
let s:floaterm_parameters = {}
let s:floaterm_parameters.right = " --wintype=vsplit --width=0.382"
let s:floaterm_parameters.belowright = " --wintype=split --height=0.2"
if g:has_popup_floating
    let s:floaterm_parameters.center = " --wintype=float --width=0.8 --height=0.8"
    let s:floaterm_parameters.topright = " --wintype=float --width=0.382 --height=0.618"
endif
function! s:floaterm_select_pos()
    let positions = ['Right', 'Belowright', 'Center', 'Topright']
    if g:has_popup_floating == 0
        let positions = positions[:1]
    endif
    let title = 'Choose a Floaterm Position'
    let pos = tolower(ChooseOne(positions, title, 0))
    if empty(pos)
        return
    endif
    let position = " --postion=" . pos
    let cmd = "FloatermNew" . s:floaterm_parameters[pos] . position
    execute cmd
endfunction
command! FloatermSelectNew call s:floaterm_select_pos()
nnoremap <silent><M-+> :FloatermSelectNew<Cr>
tnoremap <silent><M-+> <C-\><C-n>:FloatermSelectNew<Cr>
inoremap <silent><M-+> <C-o>:FloatermSelectNew<Cr>
" -----------------------------
" show floaterm
" -----------------------------
function! s:floaterm_list()
    let bufs = floaterm#buflist#gather()
    let cnt = len(bufs)
    if cnt == 0
        let no_msg = "No floaterm windows"
        if Installed('vim-quickui')
            call quickui#textbox#open([no_msg], {})
        else
            call preview#errmsg(no_msg)
        endif
        return
    endif
    let content = []
    for bufnr in bufs
        let title = getbufvar(bufnr, 'floaterm_title')
        if title ==# "floaterm($1/$2)"
            let cur = index(bufs, bufnr) + 1
            let title = substitute(title, '$1', cur, 'gm')
            let title = substitute(title, '$2', cnt, 'gm')
        endif
        let postion = getbufvar(bufnr, 'floaterm_position')
        let wintype = getbufvar(bufnr, 'floaterm_wintype')
        let cmd     = getbufvar(bufnr, 'floaterm_cmd')
        let open_cmd = printf('call floaterm#terminal#open_existing(%s)', bufnr)
        if Installed('vim-quickui')
            let title = title . "@" . wintype . '/' .  postion . ' ' .  cmd
            let line = [title, open_cmd]
        else
            let title = title . "@" . wintype . '/' .  postion
            let line = {}
            let line.bufnr = bufnr
            let line.text = title
            let line.pattern = open_cmd
        endif
        call add(content, line)
    endfor
    if Installed('vim-quickui')
        let opts = {'title': 'All floaterm buffers', 'w': 64}
        call quickui#listbox#open(content, opts)
    else
        call setqflist(content)
        execute "belowright copen" . g:asyncrun_open
    endif
endfunc
command! FloatermList call s:floaterm_list()
nnoremap <silent><M-=> :FloatermList<Cr>
tnoremap <silent><M-=> <C-\><C-n>:FloatermList<Cr>
inoremap <silent><M-=> <C-o>:FloatermList<Cr>
